﻿using B.S.Base.Read.API.Application.Command;
using B.S.Base.Read.API.Dto;
using MediatR;
using Microsoft.EntityFrameworkCore;
using WEBAPI.Damoin;
using WEBAPI.ErroeCode;
using WEBAPI.Indaseture.Infasetures;

namespace B.S.Base.Read.API.Application.CommandHandLer
{
    public class GetMeunCommandHandLer : IRequestHandler<GetMeunCommand, APIResult<APIPaing<List<MenuModel>>>>
    {
        IBaseRepostiy<MenuModel> _menuModel;

        public GetMeunCommandHandLer(IBaseRepostiy<MenuModel> menuModel)
        {
            _menuModel = menuModel;
        }

        public async Task<APIResult<APIPaing<List<MenuModel>>>> Handle(GetMeunCommand request, CancellationToken cancellationToken)
        {

            var list = _menuModel.GetAll().Where(x => !x.IsDeleted);
            int totalCount =  list.Count();
            int totalPage = (int)Math.Ceiling(totalCount * 1.0 / request.PageSize);
            //分页
            list = list.OrderByDescending(x => x.Id).Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize);
            return new APIResult<APIPaing<List<MenuModel>>>
            {
                Code = ResultCode.Ok,
                Data = new APIPaing<List<MenuModel>>
                {
                    Data=list.ToList(),
                    totalCount=totalCount,
                    totalPage=totalPage,
                },
                Message = "获取成功"
            };
       }
    }
}
